﻿import { createRouter, createWebHistory } from 'vue-router'
import LoginForm from '../components/LoginForm.vue'
import MainLayout from '../layouts/MainLayout.vue'

const routes = [
  { path: '/login', name: 'login', component: LoginForm },
  {
    path: '/',
    component: MainLayout,
    children: [
      { path: '', redirect: '/home' },
      { path: 'home', name: 'home', component: () => import('../modules/home/Index.vue'), meta: { title: '首页' } },

      { path: 'sales', name: 'sales', component: () => import('../modules/sales/Index.vue'), meta: { title: '销售管理' } },
      { path: 'sales/overview', name: 'sales-overview', component: () => import('../modules/sales/SalesOverview.vue'), meta: { title: '销售总览', parent: 'sales' } },
      { path: 'sales/customers', name: 'sales-customers', component: () => import('../modules/sales/Customers.vue'), meta: { title: '客户管理', parent: 'sales' } },
      { path: 'sales/orders', name: 'sales-orders', component: () => import('../modules/sales/Orders.vue'), meta: { title: '订单管理', parent: 'sales' } },
      { path: 'sales/attachments', name: 'sales-attachments', component: () => import('../modules/sales/AttachmentManagement.vue'), meta: { title: '附件管理', parent: 'sales' } },
      { path: 'sales/return', name: 'sales-return', component: () => import('../modules/sales/SalesReturn.vue'), meta: { title: '订单退货', parent: 'sales' } },

      { path: 'procure', name: 'procure', component: () => import('../modules/procure/Index.vue'), meta: { title: '采购管理' } },
      { path: 'procure/suppliers', name: 'procure-suppliers', component: () => import('../modules/procure/Suppliers.vue'), meta: { title: '供应商管理', parent: 'procure' } },
      { path: 'procure/purchase', name: 'procure-purchase', component: () => import('../modules/procure/Purchase.vue'), meta: { title: '采购订单管理', parent: 'procure' } },
      { path: 'procure/audit', name: 'procure-audit', component: () => import('../modules/procure/Audit.vue'), meta: { title: '采购审核管理', parent: 'procure' } },
      { path: 'procure/return-order', name: 'procure-return-order', component: () => import('../modules/procure/ReturnOrder.vue'), meta: { title: '采购退货管理', parent: 'procure' } },
      { path: 'procure/return-audit', name: 'procure-return-audit', component: () => import('../modules/procure/ReturnOrderAudit.vue'), meta: { title: '退货审核管理', parent: 'procure' } },
      { path: 'procure/purchase-detail', name: 'procure-purchase-detail', component: () => import('../views/procure/PurchaseOrderDetail.vue'), meta: { title: '采购明细', parent: 'procure' } },
      { path: 'procure/return-detail', name: 'procure-return-detail', component: () => import('../views/procure/PurchaseReturnDetail.vue'), meta: { title: '采购退货明细', parent: 'procure' } },
      { path: 'procure/statistics', name: 'procure-statistics', component: () => import('../views/procure/PurchaseStatistics.vue'), meta: { title: '商品采购统计', parent: 'procure' } },

      { path: 'ware', name: 'ware', component: () => import('../modules/ware/Index.vue'), meta: { title: '仓储管理' } },
      { path: 'ware/in', name: 'ware-in', component: () => import('../modules/ware/In.vue'), meta: { title: '入库管理', parent: 'ware' } },
      { path: 'ware/out', name: 'ware-out', component: () => import('../modules/ware/Out.vue'), meta: { title: '出库管理', parent: 'ware' } },
      { path: 'ware/inventory', name: 'ware-inventory', component: () => import('../modules/ware/Inventory.vue'), meta: { title: '库存盘点', parent: 'ware' } },
      { path: 'ware/warehouse', name: 'ware-warehouse', component: () => import('../modules/ware/Warehouse.vue'), meta: { title: '仓库管理', parent: 'ware' } },
      { path: 'ware/inbound-records', name: 'ware-inbound-records', component: () => import('../modules/ware/InboundRecords.vue'), meta: { title: '入库记录', parent: 'ware' } },
      { path: 'ware/outbound-records', name: 'ware-outbound-records', component: () => import('../modules/ware/OutboundRecords.vue'), meta: { title: '出库记录', parent: 'ware' } },
      { path: 'ware/stock-count', name: 'ware-stock-count', component: () => import('../modules/ware/StockCount.vue'), meta: { title: '库存盘点', parent: 'ware' } },
      { path: 'ware/stock-count-audit', name: 'ware-stock-count-audit', component: () => import('../modules/ware/StockCountAudit.vue'), meta: { title: '盘点审核', parent: 'ware' } },
      { path: 'ware/stock-transfer', name: 'ware-stock-transfer', component: () => import('../modules/ware/Transfer.vue'), meta: { title: '库存调拨', parent: 'ware' } },
      { path: 'ware/transfer-audit', name: 'ware-transfer-audit', component: () => import('../modules/ware/TransferAudit.vue'), meta: { title: '调拨审核', parent: 'ware' } },
      { path: 'ware/inventory-stats', name: 'ware-inventory-stats', component: () => import('../modules/ware/InventoryStats.vue'), meta: { title: '库存统计', parent: 'ware' } },

      { path: 'ware/inventory', name: 'ware-inventory', component: () => import('../modules/ware/Inventory.vue'), meta: { title: '库存管理', parent: 'ware' } },

      { path: 'finance', name: 'finance', component: () => import('../modules/finance/Index.vue'), meta: { title: '财务管理' } },
      { path: 'finance/order-payment', name: 'finance-order-payment', component: () => import('../modules/finance/OrderPayment.vue'), meta: { title: '订单付款', parent: 'finance' } },
      { path: 'finance/manual-payment', name: 'finance-manual-payment', component: () => import('../modules/finance/ManualPayment.vue'), meta: { title: '手动付款', parent: 'finance' } },
      { path: 'finance/payment-approval', name: 'finance-payment-approval', component: () => import('../modules/finance/PaymentApproval.vue'), meta: { title: '付款审批', parent: 'finance' } },
      { path: 'finance/payment-record', name: 'finance-payment-record', component: () => import('../modules/finance/PaymentRecord.vue'), meta: { title: '付款记录', parent: 'finance' } },
      { path: 'finance/order-collection', name: 'finance-order-collection', component: () => import('../modules/finance/OrderCollection.vue'), meta: { title: '订单回款', parent: 'finance' } },
      { path: 'finance/collection-record', name: 'finance-collection-record', component: () => import('../modules/finance/CollectionRecord.vue'), meta: { title: '收款记录', parent: 'finance' } },
      { path: 'finance/accounts-receivable', name: 'finance-accounts-receivable', component: () => import('../modules/finance/AccountsReceivable.vue'), meta: { title: '应收账款', parent: 'finance' } },
      { path: 'finance/accounts-payable', name: 'finance-accounts-payable', component: () => import('../modules/finance/AccountsPayable.vue'), meta: { title: '应付账款', parent: 'finance' } },
      { path: 'finance/supplier-reconciliation', name: 'finance-supplier-reconciliation', component: () => import('../modules/finance/SupplierReconciliation.vue'), meta: { title: '供应商对账单', parent: 'finance' } },
      { path: 'finance/customer-reconciliation', name: 'finance-customer-reconciliation', component: () => import('../modules/finance/CustomerReconciliation.vue'), meta: { title: '客户对账单', parent: 'finance' } },

      { path: 'commodity', name: 'commodity', component: () => import('../modules/commodity/Index.vue'), meta: { title: '商品管理' } },
      { path: 'commodity/list', name: 'commodity-list', component: () => import('../modules/commodity/List.vue'), meta: { title: '商品档案', parent: 'commodity' } },
      { path: 'commodity/category', name: 'commodity-category', component: () => import('../modules/commodity/Category.vue'), meta: { title: '商品分类', parent: 'commodity' } },

      { path: 'retail', name: 'retail', component: () => import('../modules/retail/Index.vue'), meta: { title: '零售管理' } },
      { path: 'retail/outbound', name: 'retail-outbound', component: () => import('../modules/retail/outbound/Index.vue'), meta: { title: '零售出库', parent: 'retail' } },
      { path: 'retail/returns', name: 'retail-returns', component: () => import('../modules/retail/returns/Index.vue'), meta: { title: '零售退货', parent: 'retail' } },
      { path: 'retail/tracking', name: 'retail-tracking', component: () => import('../modules/retail/tracking/Index.vue'), meta: { title: '订单跟踪', parent: 'retail' } },
      { path: 'retail/products', name: 'retail-products', component: () => import('../modules/retail/products/Index.vue'), meta: { title: '零售商品', parent: 'retail' } },

      // 零售管理创建表单路由
      { path: 'retail/outbound/create', name: 'retail-outbound-create', component: () => import('../modules/retail/outbound/CreateForm.vue'), meta: { title: '新建零售出库单', parent: 'retail' } },
      { path: 'retail/returns/create', name: 'retail-returns-create', component: () => import('../modules/retail/returns/CreateForm.vue'), meta: { title: '新建零售退货单', parent: 'retail' } },
      { path: 'retail/products/create', name: 'retail-products-create', component: () => import('../modules/retail/products/CreateForm.vue'), meta: { title: '添加零售商品', parent: 'retail' } },

      { path: 'system', name: 'system', component: () => import('../modules/system/Index.vue'), meta: { title: '系统管理' } },
      // 用户管理、角色管理、菜单管理已移除
      { path: 'system/profile', name: 'system-profile', component: () => import('../modules/system/UserProfile.vue'), meta: { title: '个人信息', parent: 'system' } },
      { path: 'system/change-password', name: 'system-change-password', component: () => import('../modules/system/ChangePassword.vue'), meta: { title: '修改密码', parent: 'system' } }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.name !== 'login' && !token) {
    next({ name: 'login' })
  } else if (to.name === 'login' && token) {
    next({ name: 'home' })
  } else {
    next()
  }
})

export default router
